<?php
/* 
 * Copyright (c) 2011 Antoine d'Otreppe de Bouvette
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

class ReflectionDocumentedProperty extends ReflectionProperty {
    /**
     * @var DocComment
     */
    private $docComment;
    
    /**
     * @var ReflectionDocumentedMethod
     */
    private $accessor;
    
    /**
     * @var ReflectionDocumentedMethod
     */
    private $mutator;
    
    /**
     *
     * @param string $class
     * @param string $name
     * @param ReflectionDocumentedMethod $accessor
     * @param ReflectionDocumentedMethod $mutator 
     */
    public function __construct($class, $name,
            ReflectionDocumentedMethod $accessor = null,
            ReflectionDocumentedMethod $mutator = null) {
        parent::__construct($class, $name);
        
        $this->accessor = $accessor;
        $this->mutator = $mutator;
    }
    
    /**
     * @return DocComment
     */
    public function getDocComment() {
        if ($this->docComment === null) {
            $this->docComment = DocComment::parseDocComment(
                    parent::getDocComment());
        }
        
        return $this->docComment;
    }
    
    /**
     * @return string the type of this property, or null if none defined 
     */
    public function getType() {
        $annotation = $this->getDocComment()->getAnnotation('var');
        
        if ($annotation !== null) {
            return $annotation->extractType();
        }
        else {
            return null;
        }
    }
    
    /**
     * Returns a brief description of this property
     * 
     * @return string 
     */
    public function getBrief() {
        return $this->getDocComment()->getBrief();
    }
    
    /**
     * @return ReflectionDocumentedMethod
     */
    public function getAccessor() {
        return $this->accessor;
    }

    /**
     *
     * @return ReflectionDocumentedMethod
     */
    public function getMutator() {
        return $this->mutator;
    }
    
    /**
     * @return boolean true if this property has a getter
     */
    public function hasAccessor() {
        return $this->accessor !== null;
    }
    
    /**
     * @return boolean true if this property has a setter
     */
    public function hasMutator() {
        return $this->mutator !== null;
    }
    
    /**
     * @return boolean true if this property has a public getter
     */
    public function hasPublicAccessor() {
        return $this->hasAccessor() && $this->accessor->isPublic();
    }
    
    /**
     * @return boolean true if this property has a public setter 
     */
    public function hasPublicMutator() {
        return $this->hasMutator() && $this->mutator->isPublic();
    }
}
